package com.litchishell;

import java.awt.*;

/**
 * @author LitchiShell
 * @description:
 * @create 2022-02-02
 */
public class Line {

    int x = 380;

    int y = 180;

    int endX = 500;

    int endY = 500;

    double length = 100;
    double minLength = 100;
    double maxLength = 750;
    double angle = 0;

    int direction = 1;

    /* 0 摇摆、1 抓取、2 收回、3 抓取返回 */
    int state;

    GameWin frame;

    Image hook = Toolkit.getDefaultToolkit().getImage("imgs/hook.png");

    Line(GameWin frame) {
        this.frame = frame;
    }

    private void lines(Graphics g) {
        endX = (int) (x + length*Math.cos(angle*Math.PI));
        endY = (int) (y + length*Math.sin(angle*Math.PI));

        g.setColor(Color.RED);
        g.drawLine(x - 1, y - 1, endX, endY);
        g.drawLine(x, y, endX, endY);
        g.drawLine(x + 1, y + 1, endX, endY);
        g.drawImage(hook, endX - 36, endY - 2, null);
    }


    void paintSelf(Graphics g) {

        /* 碰撞检测 */
        judge();

        switch (state) {
            case 0:
                if (angle < 0.1) {
                    direction = 1;
                } else if (angle > 0.9) {
                    direction = -1;
                }

                angle = angle + 0.005 * direction;
                lines(g);
                break;
            case 1:
                if (length < maxLength) {
                    length = length + 5;
                    lines(g);
                } else {
                    state = 2;
                }
                break;
            case 2:
                if (length > minLength) {
                    length = length - 5;
                    lines(g);
                } else {
                    state = 0;
                }
                break;
            case 3:
                int weight = 1;
                if (length > minLength) {
                    length = length - 10;
                    lines(g);
                    for (CommonObject object : this.frame.objects) {
                        if (object.flag) {
                            weight = object.weight;
                            object.x = endX - object.getWidth() / 2;
                            object.y = endY;

                            if (length <= minLength) {
                                object.x = -150;
                                object.y = -150;
                                object.flag = false;
                                Background.OPTION_FLAG = false;
                                Background.SUM += object.integral;
                                state = 0;
                            }

                            if (Background.OPTION_FLAG) {
                                if (object.type == 1) {
                                    weight = 1;
                                }
                                if (object.type == 2) {
                                    object.x = -150;
                                    object.y = -150;
                                    object.flag = false;
                                    Background.OPTION_FLAG = false;
                                    state = 2;
                                }
                            }
                        }
                    }

                    try {
                        Thread.sleep(weight);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                break;
            default:
        }
    }

    void judge() {
        for (CommonObject object : this.frame.objects) {
            if (endX > object.x && endX < object.x + object.width &&
                    endY > object.y && endY < object.y + object.height) {
                state = 3;
                object.flag = true;
            }
        }
    }

    void reGame() {
        angle = 0;
        length = 100;
    }
}
